package com.gzq.learn.springbootdemo.controller;

import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gzq.learn.springbootdemo.domain.UserDTO;
import com.gzq.learn.springbootdemo.domain.dao.jpa.UserRepository;

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping("/getuser2")
    @Cacheable(value = "userkey") // 如果redis中已经保存了该值，则不会继续走方法了。
    public UserDTO getUser() {
        UserDTO user = userRepository.findByUserName("bb2");
        System.out.println(user);
        return user;
    }

    @RequestMapping("/uid")
    String uid(HttpSession session) {
        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        session.setAttribute("uid", uid);
        return session.getId();
    }

}
